
	**************
	**************
	
	** Tanzania weights
		
		** wave 1 weights
		
			use "$dir/rawdata/LSMS Tanzania/2008/SEC_A_T.dta", clear
		
			keep hhid hh_weight hh_weight_trimmed
			gen weights_survey=hh_weight_trimmed
			
			gen wave="w1"
			tempfile w1
			save `w1', replace
			
		** wave 2 weights
			
			use "$dir/rawdata/LSMS Tanzania/2010/HH_SEC_A.dta", clear
			
			keep y2_hhid y2_weight
			gen hhid=y2_hhid
			gen weights_survey=y2_weight
			
			gen wave="w2"
			tempfile w2
			save `w2', replace
			
		** wave 3 weights
			
			use "$dir/rawdata/LSMS Tanzania/2012/HH_SEC_A.dta", clear
			
			keep y3_hhid y3_weight
			gen hhid=y3_hhid
			gen weights_survey=y3_weight
			
			gen wave="w3"
			tempfile w3
			save `w3', replace
			
		** wave 4 weights
		
			use "$dir/rawdata/LSMS Tanzania/2014/hh_sec_a.dta", clear
			
			keep y4_hhid y4_weights
			gen hhid=y4_hhid
			gen weights_survey=y4_weights
			
			gen wave="w4"
			tempfile w4
			save `w4', replace
			
		** wave 5 weights
		
			use "$dir/rawdata/LSMS Tanzania/2020/hh_sec_a.dta", clear
			
			keep y5_hhid y5_crossweight y5_panelweight 
			gen hhid=y5_hhid
			gen weights_survey=y5_crossweight
			
			gen wave="w5"
			tempfile w5
			save `w5', replace
			
		** append waves
			
			use `w1', clear
			append using `w2'
			append using `w3'
			append using `w4'
			append using `w5'
			
			gen country="Tanzania"
			
			tempfile tanzania
			save `tanzania', replace
			
	**************
	**************
	
	** Uganda weights
		
		** wave 1 (2009)
		
			use "$dir/rawdata/LSMS Uganda/2009/2009_GSEC1.dta", clear
			
			keep HHID wgt09wosplits wgt09
			gen hhid=HHID
			gen weights_survey=wgt09
			
			gen wave="w1"
			tempfile w1
			save `w1', replace
			
		** wave 2 (2010)
			
			use "$dir/rawdata/LSMS Uganda/2010/GSEC1.dta", clear
			
			keep HHID wgt10
			gen hhid=HHID
			gen weights_survey=wgt10
			
			gen wave="w2"
			tempfile w2
			save `w2', replace
			
		** wave 3 (2011)

			use "$dir/rawdata/LSMS Uganda/2011/GSEC1.dta", clear
			
			keep HHID mult
			gen hhid=HHID
			gen weights_survey=mult // documentation doesn't point to which weight to use, this seems the be the only available
			
			gen wave="w3"
			tempfile w3
			save `w3', replace
			
		** wave 4 (2013)
		
			use "$dir/rawdata/LSMS Uganda/2013/GSEC1.dta", clear
			
			keep HHID HHID_old wgt_X wgt
			gen hhid=HHID
			gen weights_survey=wgt_X
			
			gen wave="w4"
			tempfile w4
			save `w4', replace
			
		** wave 5 (2015)
			
			use "$dir/rawdata/LSMS Uganda/2015/gsec1.dta", clear
			
			keep HHID hwgt_W4_W5 hwgt_W5 h_xwgt_W5
			gen hhid=HHID
			gen weights_survey=h_xwgt_W5  // documentation doesn't point to which weight to use, this seems the be the only available
			
			gen wave="w5"
			tempfile w5
			save `w5', replace
			
		** wave 6 (2018)

			use "$dir/rawdata/LSMS Uganda/2018/HH/GSEC1.dta", clear
			
			keep hhid t0_hhid hwgt_W7 wgt hwgt_wc
			gen weights_survey=wgt // documentation doesn't point to which weight to use, this seems the be the best choice
			
			gen wave="w6"
			tempfile w6
			save `w6', replace
			
		** wave 7 (2019)
		
			use "$dir/rawdata/LSMS Uganda/2019/HH/gsec1.dta", clear
			
			keep hhid hhidold wgt
			gen weights_survey=wgt // documentation doesn't point to which weight to use, this seems the be the best choice
			
			gen wave="w7"
			tempfile w7
			save `w7', replace
			
		** append waves 
			use `w1', clear
			append using `w2'
			append using `w3'
			append using `w4'
			append using `w5'
			append using `w6'
			append using `w7'
			
			gen country="Uganda"
			compress
			
			tempfile uganda
			save `uganda', replace
			
	**************
	**************
	
	** Ethiopia weights
	
		** wave 2
			use "$dir/rawdata/LSMS Ethiopia/2013/sect1_hh_w2.dta", clear
			
			keep household_id2 pw2
			duplicates drop
			gen hhid=household_id2
			gen weights_survey=pw2
			
			gen wave="w2"
			tempfile w2
			save `w2', replace
			
		** wave 3
			use "$dir/rawdata/LSMS Ethiopia/2015/Household/sect1_hh_w3.dta", clear
			
			keep household_id2 pw_w3
			duplicates drop
			gen hhid=household_id2
			gen weights_survey=pw_w3
			
			gen wave="w3"
			tempfile w3
			save `w3', replace
		
		** wave 4
		
			use "$dir/rawdata/LSMS Ethiopia/2018/sect1_hh_w4.dta", clear
			
			keep household_id pw_w4
			duplicates drop
			gen hhid=household_id
			gen weights_survey=pw_w4
			
			gen wave="w4"
			tempfile w4
			save `w4', replace
			
		** append waves
			
			use `w2', clear
			append using `w3'
			append using `w4'
			
			gen country="Ethiopia"
			compress
			
			tempfile ethiopia
			save `ethiopia', replace
	
	**************
	**************
	
	** Nigeria weights
	
		** wave w1p
		
			use "$dir/rawdata/LSMS Nigeria/2010/Post Planting Wave 1/Household/secta_plantingw1.dta", clear
			
			keep hhid wt_wave1 wt_combined 
			gen weights_survey=wt_wave1
			
			gen wave="w1p"
			tempfile w1p
			save `w1p', replace
		
		** wave w1h
		
			use "$dir/rawdata/LSMS Nigeria/2010/Post Harvest Wave 1/Household/secta_harvestw1.dta", clear
			
			keep hhid wt_wave1 wt_combined 
			gen weights_survey=wt_wave1
			
			gen wave="w1h"
			tempfile w1h
			save `w1h', replace
		
		** wave w2p
		
			use "$dir/rawdata/LSMS Nigeria/2012/Post Planting Wave 2/Household/secta_plantingw2.dta", clear
		
			keep hhid wt_wave2 wt_combined
			gen weights_survey=wt_wave2
			
			gen wave="w2p"
			tempfile w2p
			save `w2p', replace
			
		** wave w2h
		
			use "$dir/rawdata/LSMS Nigeria/2012/Post Harvest Wave 2/Household/secta_harvestw2.dta", clear
			
			keep hhid wt_wave2 wt_combined
			gen weights_survey=wt_wave2
			
			gen wave="w2h"
			tempfile w2h
			save `w2h', replace
		
		** wave w3p
		
			use "$dir/rawdata/LSMS Nigeria/2015/secta_plantingw3.dta", clear
			
			keep hhid wt_wave3 wt_w1_w2_w3
			gen weights_survey=wt_wave3
			
			gen wave="w3p"
			tempfile w3p
			save `w3p', replace
		
		** wave w3h
		
			use "$dir/rawdata/LSMS Nigeria/2015/secta_harvestw3.dta", clear
			
			keep hhid wt_wave3 wt_w1_w2_w3
			gen weights_survey=wt_wave3
			
			gen wave="w3h"
			tempfile w3h
			save `w3h', replace
		
		** wave w4p
		
			use "$dir/rawdata/LSMS Nigeria/2018/secta_plantingw4.dta", clear
			
			keep hhid wt_wave4 wt_longpanel
			gen weights_survey=wt_wave4
			
			gen wave="w4p"
			tempfile w4p
			save `w4p', replace
		
		** wave w4h
		
			use "$dir/rawdata/LSMS Nigeria/2018/secta_harvestw4.dta", clear
			
			keep hhid wt_wave4 wt_longpanel
			gen weights_survey=wt_wave4
			
			gen wave="w4h"
			tempfile w4h
			save `w4h', replace
			
		** append waves
		
			use `w1p', clear
			append using `w1h'
			append using `w2p'
			append using `w2h'
			append using `w3p'
			append using `w3h'
			append using `w4p'
			append using `w4h'
			
			gen country="Nigeria"
			compress
			
			tostring hhid, replace
			
			tempfile nigeria
			save `nigeria', replace
			
	**************
	**************
	
	** Malawi weights
	
		** wave 2
			
			use "$dir/rawdata/LSMS Malawi/2004/ihs2_household.dta", clear
			
			keep case_id hhwght
			gen hhid=case_id
			gen weights_survey=hhwght
			
			gen wave="w2"
			tempfile w2
			save `w2', replace
			
		** wave 3
			
			use "$dir/rawdata/LSMS Malawi/2010/Full_Sample/Household/hh_mod_a_filt.dta", clear
			
			keep case_id hh_wgt
			gen hhid=case_id
			gen weights_survey=hh_wgt
			
			gen wave="w3"
			tempfile w3
			save `w3', replace
		
		** wave 4
		
			use "$dir/rawdata/LSMS Malawi/2016/household/hh_mod_a_filt.dta", clear
			
			keep case_id hh_wgt
			gen hhid=case_id
			gen weights_survey=hh_wgt
			
			gen wave="w4"
			tempfile w4
			save `w4', replace
		
		** wave 5
		
			use "$dir/rawdata/LSMS Malawi/2019/hh_mod_a_filt.dta", clear
			
			keep case_id hh_wgt
			gen hhid=case_id
			gen weights_survey=hh_wgt
			
			gen wave="w5"
			tempfile w5
			save `w5', replace
		
		** append waves
			
			use `w2', clear
			append using `w3'
			append using `w4'
			append using `w5'
			
			gen country="Malawi"
			compress
						
			tempfile malawi
			save `malawi', replace
	
	**************
	**************
	
	** Ghana weights
	
		** wave 1
		
			use "$dir/rawdata/GSPS Ghana/Wave 1/key_hhld_info.dta", clear
			
			keep FPrimary hhweight3 ppweight3
			tostring FPrimary, replace
			gen hhid=FPrimary
			gen weights_survey=hhweight3 // unclear from the documentation which to use
			
			gen wave="w1"
			tempfile w1
			save `w1', replace
			
		** wave 2
		
			use "$dir/rawdata/GSPS Ghana/Wave 2/01d_background.dta", clear
			
			keep FPrimary
			gen hhid=FPrimary
			duplicates drop
			
			merge 1:1 hhid using `w1', keep(master matched) nogen // apply wave 1 weights
			
			replace wave="w2"
			tempfile w2
			save `w2', replace
		
		** wave 3
		
			use "$dir/rawdata/GSPS Ghana/Wave 3/00_hh_info.dta", clear
			
			keep FPrimary
			gen hhid=FPrimary
			duplicates drop
			
			merge 1:1 hhid using `w1', keep(master matched) nogen // apply wave 1 weights
			
			replace wave="w3"
			tempfile w3
			save `w3', replace
			
		** append waves 
			
			use `w1', clear
			append using `w2'
			append using `w3'
			
			gen country="Ghana"
			
			compress
						
			tempfile ghana
			save `ghana', replace
			
	**************
	**************
	
	** South Africa weights
	
		** wave 1
			
			use "$dir/rawdata/NIDS South Africa/wave1/hhderived_W1_Anon_V7.0.0.dta", clear
			
			keep w1_hhid w1_wgt w1_dwgt w1_dtwgt
			ren w1_hhid hhid
			gen weights_survey=w1_wgt
			
			gen wave="w1"
			tempfile w1
			save `w1', replace
			
		** wave 2
			
			use "$dir/rawdata/NIDS South Africa/wave2/hhderived_W2_Anon_V4.0.0.dta", clear
			
			keep w2_hhid w2_wgt w2_dwgt
			ren w2_hhid hhid
			gen weights_survey=w2_wgt
			
			gen wave="w2"
			tempfile w2
			save `w2', replace
			
		** wave 3
			use "$dir/rawdata/NIDS South Africa/wave3/hhderived_W3_Anon_V3.0.0.dta", clear
		
			keep w3_hhid w3_wgt w3_dwgt
			ren w3_hhid hhid
			gen weights_survey=w3_wgt
			
			gen wave="w3"
			tempfile w3
			save `w3', replace
			
		** wave 4
		
			use "$dir/rawdata/NIDS South Africa/wave4/hhderived_W4_Anon_V2.0.0.dta", clear
			
			keep w4_hhid w4_wgt w4_dwgt
			ren w4_hhid hhid
			gen weights_survey=w4_wgt

			gen wave="w4"
			tempfile w4
			save `w4', replace	
			
		** wave 5
		
			use "$dir/rawdata/NIDS South Africa/wave5/hhderived_W5_Anon_V1.0.0.dta", clear
			
			keep w5_hhid w5_wgt w5_wgt_extu w5_dwgt w5_dwgt_extu
			ren w5_hhid hhid
			gen weights_survey=w5_wgt
			
			gen wave="w5"
			tempfile w5
			save `w5', replace
		
		** append waves 
		
			use `w1', clear
			append using `w2'
			append using `w3'
			append using `w4'
			append using `w5'
			
			compress
			tostring(hhid), replace
			
			gen country="South Africa"
						
			tempfile southafrica
			save `southafrica', replace
			
	**************
	**************
	
	** Niger weights
	
		** wave 1
			use "$dir/data/niger_w1_individual.dta", clear
			keep hhid w1_grappe
			duplicates drop
			tempfile niger_hhid_w1
			save `niger_hhid_w1', replace
			
			use "$dir/rawdata/EHCVM Niger/2011/Ponderation_Finale_31_05_2013_en.dta", clear
			ren grappe w1_grappe
			merge 1:m w1_grappe using `niger_hhid_w1', assert(matched) nogen
			
			gen weights_survey=hhweight
			keep hhid weights_survey
			gen wave="w1"
			tempfile w1
			save `w1', replace
			
		** wave 2
			use "$dir/data/niger_w2_individual.dta", clear
			keep hhid w2_grappe
			duplicates drop
			tempfile niger_hhid_w2
			save `niger_hhid_w2', replace
			
			use "$dir/rawdata/EHCVM Niger/2014/ECVMA2014welfare_T1c.dta", clear
			gen grappe_str=string(GRAPPE)
			replace grappe_str="00"+grappe_str if strlen(grappe_str)==1
			replace grappe_str="0"+grappe_str if strlen(grappe_str)==2
			gen menage_str=string(MENAGE)
			replace menage_str="0"+menage_str if strlen(menage_str)==1
			gen extension_str=string(EXTENSION)
			gen hhid=grappe_str+menage_str+extension_str // hhid for consistency with other countries
			
			merge 1:1 hhid using `niger_hhid_w2', assert(matched) nogen
			
			gen weights_survey=hhweight
			keep hhid weights_survey
			gen wave="w2"
			tempfile w2
			save `w2', replace
			
		** wave 3
			use "$dir/data/niger_w3_individual.dta", clear
			keep hhid w3_grappe
			duplicates drop
			tempfile niger_hhid_w3
			save `niger_hhid_w3', replace
		
			use "$dir/rawdata/EHCVM Niger/2018/ehcvm_ponderations_ner2018.dta", clear
			ren grappe w3_grappe
			merge 1:m w3_grappe using `niger_hhid_w3', assert(matched) nogen
			
			tostring(hhid), replace
			
			gen weights_survey=hhweight
			keep hhid weights_survey
			gen wave="w3"
			
			tempfile w3
			save `w3', replace
			
		** append waves 
			use `w1', clear
			append using `w2'
			append using `w3'
			gen country="Niger"
			
		tempfile niger
		save `niger', replace
	
	**************
	**************
	
		** append countries
	
			use `tanzania', clear
			append using `uganda'
			append using `ethiopia'
			append using `nigeria'
			append using `malawi'
			append using `ghana'
			append using `southafrica'
			append using `niger'
			
			order country wave hhid weights_survey
			keep country wave hhid weights_survey
		
			isid country wave hhid
			
		** flag missing cases
		
			gen weights_survey_m=missing(weights_survey)
			tab country, sum(weights_survey_m)
			
		** save
		
			replace country="tza" if country=="Tanzania"
			replace country="uga" if country=="Uganda"
			replace country="nga" if country=="Nigeria"
			replace country="eth" if country=="Ethiopia"
			replace country="mlw" if country=="Malawi" 
			replace country="gha" if country=="Ghana"
			replace country="saf" if country=="South Africa"
			replace country="niger" if country=="Niger"
			
			save "$dir/data/weights.dta", replace
